package com.ouran.controller.information;


import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouran.model.information.Information;
import com.ouran.service.information.informationService;
import com.ouran.util.htmlUtil;

/**
 * Information模块Controller
 * 
 * @version 1.0 Date 2018/09/17
 */

@Controller
@RequestMapping("/information")
public class informationController {
		
	Logger logger = Logger.getLogger(informationController.class);
	
	/**
	 * 引用文章资讯模块Service
	 */
	@Resource
	informationService informationserviceimpl;

	@Autowired
	HttpServletRequest request;
	
	@Autowired
	HttpServletResponse response;
	
	
	/**
	 * 访问登陆页面
	 * 
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/releas", method = RequestMethod.GET)
	public String releas()throws Exception {
		
		return "information/index";
	}
	
	
	/**
	 * 文章资讯列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getInformationManage", method = RequestMethod.GET)
	public @ResponseBody List<Information> getInformationManage()throws Exception{
		response.setHeader("Access-Control-Allow-Origin", "*");
		return informationserviceimpl.getInformationManage();
	}
	
	
	/**
	 * 新增文章
	 * 
	 * @param information 资讯文章实体
	 * @return 页面地址
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/addArticle", method = RequestMethod.POST)
	public String addInformation(@ModelAttribute("information") Information infor) throws UnsupportedEncodingException {
		
		infor.setArticle(new String(infor.getArticle().getBytes("ISO-8859-1"),"UTF-8"));
		infor.setAutor(new String(infor.getAutor().getBytes("ISO-8859-1"),"UTF-8"));
		
		//通过获取图片路径，替换src的base64,存入数据库
		infor.setArticle(htmlUtil.imgToBase64(infor.getArticle(),"p"));
		
		
		infor.setAutor(infor.getAutor());
		informationserviceimpl.addInfor(infor);
		return "redirect:releas";
	}
}
